package cn.johnyu.service;

import cn.johnyu.mapper.AccountMapper;
import cn.johnyu.pojo.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;


@Service
public class AccountServiceImpl implements AccountService{
    @Autowired
    private AccountMapper accountMapper;
    @Transactional
    @Override
    public int transfer(int from, int to, int amount) {
        Account account1 = accountMapper.loadAccount(from);
        Account account2 = accountMapper.loadAccount(to);
        account1.setBalance(account1.getBalance()-amount);
        account2.setBalance(account2.getBalance()+amount);
        int rs1=accountMapper.updateAccount(account1);
//        int m=1/0;
        int rs2=accountMapper.updateAccount(account2);
        return rs1+rs2;
    }
}
